//
//  GPPhotoCollectionViewCell.m
//  Runone
//
//  Created by runone01 on 16/9/29.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "GPPhotoCollectionViewCell.h"
#import "UIColor+Addition.h"
@implementation GPPhotoCollectionViewCell{
    NSInteger currentIndex;
    
}

- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier{
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:self.imageView];
    self.selectButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-40,40, 40, 40)];
    self.selectButton.backgroundColor = [UIColor redColor];
    [self addSubview:self.selectButton];
}
- (void)setDataInfo:(id)data index:(NSInteger)index currentIndex:(NSInteger)currentSelectIndex{
    self.imageView.image = (UIImage *)data;
    self.selectButton.tag = index;
    currentIndex = currentSelectIndex;
    [self setSelectButtonState:index];
}

- (IBAction)clickSelect:(id)sender {
    UIButton *btn = sender;
    if (btn.tag>0) {
        //取消选择
        if (self.SelectBlock) {
            self.SelectBlock(0,self.tag);
        }
        return;
    }
    if (self.SelectBlock) {
        self.SelectBlock(currentIndex+1,self.tag);
    }
}

- (void)setSelectButtonState:(NSInteger)index{
    
    if (index>0) {
        [self beSelectedIndex:index];
    }else{
        [self select];
    }
}

- (void)beSelectedIndex:(NSInteger)index{
    [self.selectButton setBackgroundImage:[UIImage imageNamed:@"check_blue"] forState:UIControlStateNormal];
    [self.selectButton setTintColor:[UIColor whiteColor]];
    [self.selectButton setTitle:[NSString stringWithFormat:@"%d",index] forState:UIControlStateNormal];
    self.backView.hidden = NO;
    self.backView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
}

- (void)select{
    [self.selectButton setBackgroundImage:[UIImage imageNamed:@"uncheck_gray"] forState:UIControlStateNormal];
    [self.selectButton setTitle:nil forState:UIControlStateNormal];
    self.backView.hidden = YES;
}


@end
